package com.sunday.volunteer.utils;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.common.widgets.UIAlertView;
import com.sunday.volunteer.R;
import com.sunday.volunteer.entity.Cart;
import com.sunday.volunteer.http.HttpClient;
import com.sunday.volunteer.ui.ShowBigImage;
import com.sunday.volunteer.ui.WebViewActivity;
import com.sunday.volunteer.ui.activity.ActivityDetialActivity;
import com.sunday.volunteer.ui.mine.GroupDetialActivity;
import com.sunday.volunteer.ui.product.ConfirmOrderActivity;
import com.sunday.volunteer.ui.product.ProductDetailActivity;
import com.sunday.volunteer.ui.user.SendCodeActivity;
import com.sunday.volunteer.ui.user.UpdatePasswordActivity;


/**
 * Created by Pual's PC on 2016/10/18.
 */
public class BaseUtil<T> {


    public static void setEmptyLayout(EmptyLayout emptyLayout , int size , String content){
        if(size!= 0){
            emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
        }else {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
            emptyLayout.setErrorImag(R.mipmap.ic_launcher);
            emptyLayout.setErrorMessage("屋顶空空如也");
            if(!TextUtils.isEmpty(content)){
                emptyLayout.setErrorMessage(content);
            }
        }
    }

    public static void setText(TextView text , Object object , String myContent){
        String content = String.valueOf(object);
        if(TextUtils.isEmpty(content)||content.equals("null")){
            text.setText(myContent);
        }else {
            text.setText(content);
        }
    }

    public static void setText(TextView text , Object object){
        String content = String.valueOf(object);
        if(TextUtils.isEmpty(content) ||content.equals("null")){
            text.setText("");
        }else {
            text.setText(content);
        }
    }

    public static void setTextFormat(TextView text ,String string1 ,String object){
        String content = String.valueOf(object);
        if(TextUtils.isEmpty(content)||content.equals("null")){
            text.setText(String.format("%s%s" , string1 , ""));
        }else {
            text.setText(String.format("%s%s" , string1 , content));
        }
    }

    public static void setImage(ImageView imageView , String imagePath , int width , int height){
        if(TextUtils.isEmpty(imagePath)){
            imagePath = "http";
        }
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Picasso.with(imageView.getContext()).load(imagePath).resize(width ,height).centerCrop().placeholder(R.drawable.default_error).into(imageView);
    }

    abstract public static class BaseUtilClickListener implements UIAlertView.ClickListenerInterface{
        private UIAlertView view;
        public BaseUtilClickListener() {

        }
        @Override
        public void doLeft() {
            view.dismiss();
        }

        @Override
        public void doRight() {
            view.dismiss();
            doRightbtn();
        }
        abstract public void doRightbtn();

        public void setView(UIAlertView view) {
            this.view = view;
        }
    }

    public static void showDialog(Context context, String title , BaseUtilClickListener clickListener){
        final UIAlertView view = new UIAlertView(context , "温馨提示" ,title ,"取消" ,"确定");
        view.setClicklistener(clickListener);
        clickListener.setView(view);
        view.show();
    }
    public static String TITLE = "title";
    public static String URL = "url";
    public static String ID = "id";
    public static String ITEM = "item";
    public static String CONTENT = "content";
    public static String MOBILE = "mobile";
    public static String CODE = "code";
    public static String TYPE = "type";


    public static String HTML_URL = HttpClient.API_URL+"/SystemInfoController/getByType?type=";
    public static void openActivity(Context mContext ,Class<?> cls){
        Intent intent = new Intent(mContext , cls);
        mContext.startActivity(intent);
    }

    public static void openWebViewActivity(Context mContext ,String url ,String title){
        Intent intent = new Intent(mContext , WebViewActivity.class);
        intent.putExtra(URL , url);
        intent.putExtra(TITLE , title);
        mContext.startActivity(intent);
    }

    public static void openBigImage(Context mContext ,String imgPath){
        Intent intent = new Intent(mContext , ShowBigImage.class);
        intent.putExtra("imgPath" , imgPath);
        mContext.startActivity(intent);
    }





    /**
     *跳转到活动详情
     * @param mContext
     * @param id
     */
    public static void openActivityDetialActivity(Context mContext ,long id){
        Intent intent = new Intent(mContext , ActivityDetialActivity.class);
        intent.putExtra(ID , id);
        mContext.startActivity(intent);
    }


    /**
     *跳转到活动详情
     * @param mContext
     * @param id
     */
    public static void openGroupDetialActivity(Context mContext ,Long id){
        Intent intent = new Intent(mContext , GroupDetialActivity.class);
        intent.putExtra(ID , id);
        mContext.startActivity(intent);
    }

    /**
     *跳转到商品详情
     * @param mContext
     * @param id
     */
    public static void openProductDetailActivity(Context mContext ,long id){
        Intent intent = new Intent(mContext , ProductDetailActivity.class);
        intent.putExtra(ID , id);
        mContext.startActivity(intent);
    }


    /**
     *跳转到提交订单页面
     * @param mContext
     * @param item
     */
    public static void openConfirmOrderActivity(Context mContext ,Cart item){
        Intent intent = new Intent(mContext , ConfirmOrderActivity.class);
        intent.putExtra(ITEM , item);
        mContext.startActivity(intent);
    }


    /**
     * 跳转发送验证码
     * @param mContext
     * @param type
     */
    public static void openSendCodeActivity(Context mContext ,int type){
        Intent intent = new Intent(mContext , SendCodeActivity.class);
        intent.putExtra(TYPE , type);
        mContext.startActivity(intent);
    }

    /**
     * 跳转到修改密码或忘记密码
     * @param mContext
     * @param mobile
     */
    public static void openUpdatePasswordActivity(Context mContext ,String mobile ,String validateCode ,int type){
        Intent intent = new Intent(mContext , UpdatePasswordActivity.class);
        intent.putExtra(MOBILE, mobile);
        intent.putExtra(CODE , validateCode);
        intent.putExtra(TYPE , type);
        mContext.startActivity(intent);
    }




}
